<?php

###########################################
#    CLASE GENERICA DE BASE DE DATOS      #                 
###########################################

class db_tools {
	var $make_errors = true; //Activa o desactiva los errores en base de datos del sistema
	var $specialstr=array("	","\n\r","\n","\r");
	var $var_error = '';
	var $var_sql_error = '';
	var $var_trans = '0';
	
	/**
	 * limpiar la sentencia sql
	 *
	 * @param varchar $data	sentencia SQL
	 */
	function rewritedata($data){
		$data=addslashes(str_replace($this->specialstr,"",$data));
		return $data;
	}
	/**
	 * SELECT TOOL
	 *
	 * @param varchar $query	sentencia SQL
	 * @return array 			returno de valores
	 */	
	function select_tool($query){
		$result=mysql_query($query);
		$res_array=array(); $i = 0;
		if ($result) {
			if (mysql_num_rows($result) > 0){
				while($row=mysql_fetch_array($result)){
					foreach($row as $key=>$value){
						$res_array[$i][$key]=$value;
					}
					$i++;
				}
				return $res_array;
			}else{
				return null;
			}
		}else{
			if ($make_errors){
				$this->var_error = mysql_error();
				$this->var_sql_error = $this->rewritedata($query);
			}else{
				die(mysql_error()." -> ".$query);
			}
		}
	}
	/**
	 * INSERT TOOL CON RETORNO DE ID GENERADO EN LA EJECUCION DE LA SENTENCIA
	 *
	 * @param varchar $query	sentencia SQL
	 * @return bigint 			returno de valores
	 */	
	function ins_tool_return($query){
		@mysql_query($query);
		if( mysql_error() ) {
			if ($make_errors){
				$this->var_error = mysql_error();
				$this->var_sql_error = $this->rewritedata($query);
			}else{
				die(mysql_error()." -> ".$query);
			}
		}else{
			return (mysql_insert_id());
		}
	}
	/**
	 * INSERT TOOL SIN RETORNO
	 *
	 * @param varchar $query	sentencia SQL
	 */
	function ins_tool_noreturn($query){
		@mysql_query($query);
		if( mysql_error() ) {
			if ($make_errors){
				$this->var_error = mysql_error();
				$this->var_sql_error = $this->rewritedata($query);
			}else{
				die(mysql_error()." -> ".$query);
			}
		}
	}
	/**
	 * UPDATE TOOL
	 *
	 * @param varchar $query	sentencia SQL
	 */
	function upt_tool($query){
		if (mysql_query($query) == 0){
			if ($make_errors){
				$this->var_error = mysql_error();
				$this->var_sql_error = $this->rewritedata($query);
			}else{
				die(mysql_error()." -> ".$query);
			}
		}
	}
	/**
	 * DELETE TOOL
	 *
	 * @param varchar $query	sentencia SQL
	 */
	function del_tool($query){
		if (mysql_query($query) == 0){
			if ($make_errors){
				$this->var_error = mysql_error();
				$this->var_sql_error = $this->rewritedata($query);
			}else{
				die(mysql_error()." -> ".$query);
			}
		}
	}
	
	/**
	 * Begin Transaction
	 *
	 */
	function begin_tool(){
		mysql_query("BEGIN WORK") or die(mysql_error());
		$this->var_trans = '1';
	}
	
	/**
	 * Coomit Transaction
	 *
	 */
	function commit_tool(){
		mysql_query("COMMIT") or die(mysql_error());
		$this->var_trans = '0';
	}
	
	/**
	 * Rollback Transaction
	 *
	 */
	function rollback_tool(){
		mysql_query("ROLLBACK") or die(mysql_error());
	}
	
}
?>